<template>
  <div class="supplier-list">
    <!-- 搜索区域 -->
    <div class="search-section">
      <el-form :inline="true" :model="searchForm" class="search-form">
        <el-form-item label="供应商名称">
          <el-input
            v-model="searchForm.supplierName"
            placeholder="请输入供应商名称"
            clearable
          />
        </el-form-item>
        <el-form-item label="联系人">
          <el-input
            v-model="searchForm.contactName"
            placeholder="请输入联系人"
            clearable
          />
        </el-form-item>
        <el-form-item label="联系电话">
          <el-input
            v-model="searchForm.contactPhone"
            placeholder="请输入联系电话"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            搜索
          </el-button>
          <el-button icon="el-icon-refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="operation-section">
      <el-button type="primary" icon="el-icon-plus" @click="handleAdd">
        新增供应商
      </el-button>
    </div>

    <!-- 表格区域 -->
    <el-table
      v-loading="loading"
      :data="supplierList"
      border
      stripe
    >
      <el-table-column type="index" label="序号" width="80" align="center" />
      <el-table-column prop="supplierName" label="供应商名称" min-width="150" />
      <el-table-column prop="contactName" label="联系人" min-width="120" />
      <el-table-column prop="contactPhone" label="联系电话" min-width="120" />
      <el-table-column prop="email" label="邮箱" min-width="150" />
      <el-table-column prop="address" label="地址" min-width="200" />
      <el-table-column 
        prop="createdAt" 
        label="创建时间" 
        min-width="160"
        :formatter="(row) => formatDate(row.createdAt)"
      />
      <el-table-column label="操作" width="200" align="center" fixed="right">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
          >
            编辑
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页区域 -->
    <div class="pagination-section">
      <el-pagination
        :current-page.sync="pageNum"
        :page-size="pageSize"
        :total="total"
        :page-sizes="[10, 20, 50, 100]"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script>
import { getSuppliers, deleteSupplier } from '@/api/suppliers'
import { formatDate } from '@/utils/date'

export default {
  name: 'SupplierList',
  data() {
    return {
      searchForm: {
        supplierName: '',
        contactName: '',
        contactPhone: ''
      },
      supplierList: [],
      pageNum: 1,
      pageSize: 10,
      total: 0,
      loading: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    formatDate,
    async getList() {
      this.loading = true
      try {
        const params = {
          pageNum: this.pageNum,
          pageSize: this.pageSize,
          ...this.searchForm
        }
        const res = await getSuppliers(params)
        if (res.code === 200) {
          this.supplierList = res.data.list
          this.total = res.data.total
        }
      } catch (error) {
        console.error('获取供应商列表失败:', error)
        this.$message.error('获取供应商列表失败')
      } finally {
        this.loading = false
      }
    },
    handleSearch() {
      this.pageNum = 1
      this.getList()
    },
    resetSearch() {
      this.searchForm = {
        supplierName: '',
        contactName: '',
        contactPhone: ''
      }
      this.handleSearch()
    },
    handleAdd() {
      this.$router.push('/supplier/add')
    },
    handleEdit(row) {
      this.$router.push(`/supplier/edit/${row.supplierId}`)
    },
    async handleDelete(row) {
      try {
        await this.$confirm('确认删除该供应商吗？', '提示', {
          type: 'warning'
        })
        const res = await deleteSupplier(row.supplierId)
        if (res.code === 200) {
          this.$message.success('删除成功')
          this.getList()
        } else {
          this.$message.error(res.msg || '删除失败')
        }
      } catch (error) {
        if (error !== 'cancel') {
          console.error('删除供应商失败:', error)
          this.$message.error('删除失败')
        }
      }
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.pageNum = val
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
.supplier-list {
  padding: 20px;

  .search-section {
    background: #fff;
    padding: 18px 20px;
    border-radius: 4px;
    margin-bottom: 16px;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
  }

  .operation-section {
    margin-bottom: 16px;
  }

  .el-table {
    margin-bottom: 16px;
  }

  .pagination-section {
    display: flex;
    justify-content: flex-end;
    margin-top: 16px;
    padding: 10px 20px;
    background: #fff;
    border-radius: 4px;
  }
}
</style>
